package com.ruoyi.system.controller.cust;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.MaterialExp;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.cust.BasMaterial;
import com.ruoyi.system.service.cust.IBasMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/system/material")
public class BasMaterialCtr  extends BaseController {
    @Autowired
    IBasMaterialService basMaterialService;

    @GetMapping("/selectMaterialList")
    public TableDataInfo selectMaterialList(BasMaterial material) {
        startPage();
        return getDataTable(basMaterialService.list(new QueryWrapper<>(material)));
    }

    @PostMapping
    @Log(title = "物料信息", businessType = BusinessType.INSERT)
    public AjaxResult insterMaterial(@RequestBody BasMaterial material) {
        material.setCreateStaffId(SecurityUtils.getUserId());
        material.setCreateStaffName(SecurityUtils.getUsername());
        material.setCreateDate(LocalDateTime.now());
        return toAjax(basMaterialService.save(material));
    }

    @GetMapping(value = "/{matId}")
    public AjaxResult getInfo(@PathVariable("matId") Long matId){
        return success(basMaterialService.getById(matId));
    }

    @PutMapping
    @Log(title = "物料信息", businessType = BusinessType.UPDATE)
    public AjaxResult updateMaterial(@RequestBody BasMaterial material) {
        LambdaUpdateWrapper<BasMaterial> queryWrapper = new UpdateWrapper<BasMaterial>().lambda();
        queryWrapper.set(BasMaterial::getMatNo, material.getMatNo())
                .set(BasMaterial::getMatName, material.getMatName())
                .set(BasMaterial::getType, material.getType())
                .set(BasMaterial::getClName, material.getClName())
                .set(BasMaterial::getCustMatName, material.getCustMatName())
                .set(BasMaterial::getCurId, material.getCurId())
                .set(BasMaterial::getEngColor, material.getEngColor())
                .set(BasMaterial::getEngName, material.getEngName())
                .set(BasMaterial::getImagePath, material.getImagePath())//图片单独处理？
                .set(BasMaterial::getMcId, material.getMcId())
                .set(MaterialExp::getModifyDate,LocalDateTime.now())
                .set(MaterialExp::getModifyStaffId,SecurityUtils.getUserId())
                .set(MaterialExp::getModifyStaffName,SecurityUtils.getUsername())
                .eq(BasMaterial::getMatId, material.getMatId());
        return success(basMaterialService.update(queryWrapper));
    }

    @DeleteMapping("/{matIds}")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
    public AjaxResult deleteMaterial(@PathVariable Long[] matIds) {
        return toAjax(basMaterialService.removeByIds(Arrays.asList(matIds)));
    }

    @GetMapping("/updatePass/{matId}")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    public void updatePass(@PathVariable Long matId) {
        BasMaterial byId = basMaterialService.getById(matId);
        LambdaUpdateWrapper<BasMaterial> updateWrapper = new UpdateWrapper<BasMaterial>()
                .lambda()
                .set(BasMaterial::getPassStaffId, SecurityUtils.getUserId())
                .set(BasMaterial::getPassStaffName, SecurityUtils.getUsername())
                .eq(BasMaterial::getMatId, matId);
        if (Objects.isNull(byId)) throw new ServiceException("物料信息不存在");
        updateWrapper.set(byId.getPass() == 1, BasMaterial::getPass, 0)
                .set(byId.getPass() != 1, BasMaterial::getPass, 1)
                .set(byId.getPassDate() == null, BasMaterial::getPassDate, LocalDateTime.now());
        basMaterialService.update(updateWrapper);
    }


}
